package app.util.image.action
{
	import app.util.*;
	
	import flash.display.*;
	import flash.filters.*;
	import flash.geom.*;	

	public class NightVisionAction extends AbstractAction
	{
		private var brightness:Number;
		private var contrast:Number;
		private var fade:Number;
		
		public function setValue(b:Number, c:Number, f:Number):NightVisionAction
		{
			this.brightness = b;
			this.contrast = c;
			this.fade = f;
			return this;
		}
		
		public override function getDescription():String
		{
			return "Night Vision (" + this.brightness.toFixed(0) + ", " + 
						this.contrast.toFixed(0) + ", " + 
						this.fade.toFixed(0) + ")";
		}
		
		public override function getActionedBitmapData():BitmapData
		{
			var old:BitmapData = Mediator.getCurrentBitmapData();
			var ret:BitmapData = old.clone();
			
			var c:ColorMatrix = new ColorMatrix();
			c.adjustBrightness(this.brightness);
			c.adjustContrast(this.contrast);
			
            var color:uint = 0x44ff44;
            var r:Number = (color >> 16 & 255) / 192;
            var g:Number = (color >> 8 & 255) / 192;
            var b:Number = (color >> 0 & 255) / 192;
			var tintMatrix:Array =  [0.3, 0.59, 0.11, 0, 42,
							 0.3, 0.59, 0.11, 0, 2,
							 0.3, 0.59, 0.11, 0, 2,
							 0, 0, 0, 1, 0]; 
			tintMatrix[0] = tintMatrix[0] * r;
            tintMatrix[1] = tintMatrix[1] * r;
            tintMatrix[2] = tintMatrix[2] * r;
            tintMatrix[5] = tintMatrix[5] * g;
            tintMatrix[6] = tintMatrix[6] * g;
            tintMatrix[7] = tintMatrix[7] * g;
            tintMatrix[10] = tintMatrix[10] * b;
            tintMatrix[11] = tintMatrix[11] * b;
            tintMatrix[12] = tintMatrix[12] * b;
            tintMatrix[18] = 1;
            
			var shadowFilter:DropShadowFilter = new DropShadowFilter();
			shadowFilter.inner = true;
			shadowFilter.distance = 4.0;
			shadowFilter.strength = 2;
			shadowFilter.alpha = 0.6;
            shadowFilter.quality = 3;
            shadowFilter.angle = 45;
            shadowFilter.blurX = ret.width / 3;
            shadowFilter.blurY = ret.height / 3;            
		
			var noiseData:BitmapData = new BitmapData(ret.width, ret.height, true, 0);
         	noiseData.noise(0, 159, 255);
            ret.draw(noiseData, null, null, BlendMode.MULTIPLY);
					
			ret.applyFilter(ret, ret.rect, new Point(0, 0), new ColorMatrixFilter(c.matrix));
			ret.applyFilter(ret, ret.rect, new Point(0, 0), new ColorMatrixFilter(tintMatrix));
			ret.applyFilter(ret, ret.rect, new Point(0, 0), shadowFilter);
			ret.merge(old, ret.rect, new Point(0,0), 256 - fade,
				256 - fade, 256 - fade, 256 - fade);
			return ret;
		}	
		
	}
}